package cn.yitun.chat.yitunserverimapicore.business.user.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.user.UserDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserHeadDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserHeadVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserHeadService;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 11:53:11<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Controller
@ActionMapping(value = ActionTypeConstant.USER_ACTION_PUSH)
public class UserAction {

	@Resource
	private UserService userService;
	@Resource
	private UserHeadService userHeadService;

	/**
	 * Date 2019-01-20 12:29:19<br>
	 * Description 条件分页查询用户
	 * @author Xyitun
	 * @param userQuery
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage queryUserDataList(SocketSession socketSession, @JsonDefine("data") UserDto userQuery) {
		PageHelper.startPage(userQuery.getCurrentPageNo(), userQuery.getPageSize());
		userQuery.setCurrentUserId(socketSession.getKey());
		List<UserVo> userDataList = userService.queryUserDataList(userQuery);

		userService.setUserStatus(UserDto2VoConverter.INSTANCE.vo2dto(userDataList));
		return new ResultMessage(R.param("page", new PageInfo<>(userDataList)));
	}

	/**
	 * Date 2019-01-20 12:29:06<br>
	 * Description 获取用户详情
	 * @author yitun
	 * @param userQuery
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public Object getUserDataById(@JsonDefine("data") UserDto userQuery) {
		if (NumUtil.isInValid(userQuery.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		UserVo userData = userService.getUserDataById(userQuery.getId());
		userService.setUserStatus(UserDto2VoConverter.INSTANCE.vo2dto(userData));
		return new ResultMessage(R.param("user", userData));
	}

	/**
	 * Description 批量获取用户详情 <br>
	 * Date 2019-05-05 08:35:50<br>
	 * @param userQuery
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage getUserDataList(@JsonDefine("data") UserDto userQuery) {
		if (YitunCollectionUtil.isEmpty(userQuery.getIds()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		List<UserVo> userDataList = userService.getUserDataList(userQuery.getIds());

		if (YitunCollectionUtil.isNotEmpty(userDataList))
			userService.setUserStatus(UserDto2VoConverter.INSTANCE.vo2dto(userDataList));

		return new ResultMessage(R.param("list", userDataList));
	}

	/**
	 * Date 2019-01-20 19:54:10<br>
	 * Description 获取用户头像信息
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0004")
	public Object getUserHead(@JsonDefine("data") UserHeadDto query) {
		if (NumUtil.isInValid(query.getUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		UserHeadVo userHead = userHeadService.getUserHeadByUserId(query.getUserId());
		return new ResultMessage(R.param("head", userHead));
	}
}
